import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_xyf/widget/progress_dialog.dart';

abstract class BaseWidget extends StatefulWidget {
  @override
  BaseWidgetState createState() => getState();

  ///子类实现
  BaseWidgetState getState();
}

abstract class BaseWidgetState<T extends BaseWidget> extends State<T>
    with WidgetsBindingObserver {
  bool _isContentWidgetShow = false;
  bool _hasSecurityKeyboard = true;
  bool _isLoading = false;
  String title = '';
  bool hideAppbar = false;

  toggleContentShow() {
    this.setState(() {
      _isContentWidgetShow = false;
    });
  }

  BaseWidgetState({bool keyboard = true}) {
    this._hasSecurityKeyboard = keyboard;
  }

  childInitData({contentShow: false, keyboard: false}) {
    this._isContentWidgetShow = contentShow;
    this._hasSecurityKeyboard = keyboard;
  }

  @override
  void initState() {
    super.initState();
    final widgetsBinding = WidgetsBinding.instance;
    widgetsBinding!.addPostFrameCallback((callBack) {
      this.showLoading();
    });
    this.queryData();
  }

  @override
  Widget build(BuildContext context) {
    return _buildWidgetDefault();
  }

  ///构建默认视图
  Widget _buildWidgetDefault() {
    ///使用appbar，也可直接只有 body 在 body 里自定义状态栏、标题栏
    return WillPopScope(
      child: Scaffold(
        appBar: hideAppbar ? null : _buildAppBar(),
        body: _buildBody(),
      ),
      onWillPop: _requestPop,
    );
  }

  hideLoading() {
    if (this._isLoading && mounted) {
      Navigator.maybePop(context);
    }
    this.setState(() {
      this._isLoading = false;
    });
  }

  _buildAppBar() {
    return AppBar(
      title: Text(title),
    );
  }

  // 获取网络请求
  queryData();

  showEmptyWidget() {}
  showNetErrWidget() {}

  ///构建内容区
  Widget _buildBody() {
    return Container(
      ///内容区背景颜色
      color: Colors.white,
      child: Stack(
        children: <Widget>[
          Offstage(
            offstage: !_isContentWidgetShow,
            child: buildWidget(context),
          ),
          // _buildErrorWidget(),
          // _buildEmptyWidget(),
          // _buildLoadingWidget(),
        ],
      ),
    );
  }

  ///物理返回
  Future<bool> _requestPop() {
    bool b = true;
    // if (KeyboardManager.isShowKeyboard) {
    //   KeyboardManager.hideKeyboard();
    //   b = false;
    // }
    return Future.value(b);
  }

  void showLoading() {
    /// 避免重复弹出
    if (mounted && !_isLoading) {
      _isLoading = true;
      try {
        showDialog<void>(
          context: context,
          barrierDismissible: false,
          // barrierColor:
          //     const Color(0x00FFFFFF), // 默认dialog背景色为半透明黑色，这里修改为透明（1.20添加属性）
          builder: (_) {
            return WillPopScope(
              onWillPop: () async {
                // 拦截到返回键，证明dialog被手动关闭
                _isLoading = false;
                return Future.value(true);
              },
              child: ProgressDialog(
                hintText: '正在加载...',
              ),
            );
          },
        );
      } catch (e) {
        /// 异常原因主要是页面没有build完成就调用Progress。
        print(e);
      }
    }
  }

  // 子类需要实现
  Widget buildWidget(BuildContext context);
}
